package vacuum.noxray.math;

/**
 * Represents the vector equation of a line
 * @author Ryan Amos
 */
public class Line {
	private Vector position;
	private Vector direction;
	
	public Line(Vector position, Vector direction) {
		super();
		this.position = position;
		this.direction = direction;
	}
	
	public Vector[] getBlocks(){
		Vector[] ret;
		double loc[] = {position.getX(), position.getY(), position.getZ()};
		Vector finalVector = new Vector(position.getX() + direction.getX(), position.getY() + direction.getY(), position.getZ() + direction.getZ());
		int[] idx = {-1, -1, -1};
		
		//find non-zero direction vector
		if(direction.getX() == 0 && direction.getY() == 0 && direction.getZ() == 0)
			throw new ArithmeticException("Non-zero vector required.");
		
		if(Math.abs(direction.getX()) >= Math.abs(direction.getY()) && Math.abs(direction.getX()) >= Math.abs(direction.getZ())){
			idx[0] = 0;
			idx[1] = 1;
			idx[2] = 2;
		} else if (Math.abs(direction.getY()) >= Math.abs(direction.getX()) && Math.abs(direction.getY()) >= Math.abs(direction.getZ())){
			idx[0] = 1;
			idx[1] = 0;
			idx[2] = 2;
		} else if (Math.abs(direction.getZ()) >= Math.abs(direction.getX()) && Math.abs(direction.getZ()) >= Math.abs(direction.getY())){
			idx[0] = 2;
			idx[1] = 0;
			idx[2] = 1;
		} else
			throw new UnsupportedOperationException("Unknown how this point was reached. Report to developer immediately. Vector: " + direction);
		//flatten direction vector so idx[i] element of {1, -1}
		double lambda = 1.0 / Math.abs(direction.get(idx[0]));
		double[] d = new double[3];
		for (int i = 0; i < d.length; i++) {
			d[idx[i]] = lambda * direction.get(idx[i]);
		}
		
		//compute
		ret = new Vector[Math.abs(position.get(idx[0]) - finalVector.get(idx[0])) - 2];
		for (int i = 0; i < ret.length; i++) {
			int[] pos = new int[3];
			for (int j = 0; j < pos.length; j++) {
				pos[j] = (int) (loc[j] + d[j] * (i + 1));
			}
			ret[i] = new Vector(pos);
		}
		return ret;
	}
	
	public String toString(){
		return position + "t(" + direction + ")";
	}
}
